<template>
	<view class="login">
		<view class="logo_box">
			<view>
				<image src="../../static/logo.png" mode="aspectFit"></image>
			</view>
		</view>
		<view class="login_box">
			<view class="login_btn_box">
				<button class="login_btn" @click="login">手机号登录</button>
			</view>
			<view class="third_login">
				<view v-for="(item,index) in logoList" :key="index">
					<uni-icons :type="item.type" size="20" color="#ffffff" @click="thirdLogin(index)"></uni-icons>
				</view>
			</view>
			<view class="xieyi">
				<view><checkbox/>同意《用户协议》《隐私政策》《儿童隐私政策》</view>
				<view>《中国联通认证服务协议》</view>
			</view>
		</view>
		<!-- 登录弹窗 -->
		<uni-popup ref="popUp" type="center">
		   <view class="login_pop">
			   <view class="phone_box">
			   	<label for="phone">手机号</label>
			   	<input v-model="phone" id="phone" :focus="true" />
			   </view>
			  <view class="passW_box">
			  	<label for="passwd">密码</label>
			  	<input v-model="passwd" id="passwd" :password="true"/>
			  </view>
			  <view class="btn_login_box">
				  <button type="default" @click="close">取消</button>
				  <button type="primary" @click="confirm">确定</button>
			  </view>
		   </view>
		</uni-popup>
		<!-- 消息提示 -->
		<uni-popup ref="popMes" type="message">
		    <uni-popup-message :message="message">
			</uni-popup-message>
		</uni-popup>
	</view>
</template>

<script>
	import uniPopup from '@/components/uni-popup/uni-popup.vue'
	import uniPopupMessage from '@/components/uni-popup/uni-popup-message.vue'
	import uniPopupDialog from '@/components/uni-popup/uni-popup-dialog.vue'
	import {userLogin} from '@/network/user.js'
	export default {
		data() {
			return {
				logoList:[
					{type:'weixin'},
					{type:'qq'},
					{type:'weibo'}
				],
				message:'',
				phone:'',
				passwd:''
				
			}
		},
		components: {
		        uniPopup,
		        uniPopupMessage,
		        uniPopupDialog
		},
		methods: {
			// 打开登录弹窗
			login(){
				this.$refs.popUp.open()
			},
			// 关闭登录弹窗
			close(){
				this.$refs.popUp.close()
			},
			// 确定登录
			confirm(){
				if(this.phone === '' || this.passwd === ''){
					this.message = '请输入必要信息'
					this.$refs.popMes.open()
				}else {
					userLogin(this.phone,this.passwd).then(res => {
						// console.log(res)
						if(res.data.code === 200){
							uni.setStorage({
								key:"uid",
								data:res.data.account.id
							})
							uni.setStorage({
								key:'cookie',
								data:res.data.cookie,
							})
							uni.switchTab({
								url:'../profile/profile'
							})
							this.$refs.popUp.close()
						}else {
							this.message = res.data.msg
							this.$refs.popMes.open()
							
						}
					})
					
				}
			},
			// 第三方登录
			thirdLogin(index){
				if(index === 1){
					uni.getProvider({
					    service: 'oauth',
					    success: function (res) {
					        console.log(res.provider)
					        if (res.provider.indexOf('qq')) {
					            uni.login({
					                provider: 'qq',
					                success: function (loginRes) {
					                    console.log(JSON.stringify(loginRes));
										 // 获取用户信息
										    uni.getUserInfo({
										      provider: 'qq',
										      success: function (infoRes) {
												  uni.setStorage({
												  	key:'userInfo',
													data:infoRes.userInfo,
													success: () => {
														uni.switchTab({
															url:'../profile/profile'
														})
													}
												  })
										      }
										    });
					                }
					            });
					        }
					    }
					});
				}
			}
		}
	}
</script>

<style lang="scss">
	.login{
		width: 100%;
		height: 100vh;
		background-color: #EE0A24;
	}
	.logo_box{
		height: 500rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		image{
			width: 120rpx;
			height: 120rpx;
			border-radius: 50%;
		}
	}
	.login_box{
		width: 100%;
		position: fixed;
		bottom: 20rpx;
		.login_btn_box{
			margin-top: 200rpx;
			.login_btn{
				color: #EE0A24;
				width: 600rpx;
				border-radius: 40rpx;
			}
		}
		.third_login{
			width: 100%;
			height: 100rpx;
			margin-top: 20rpx;
			display: flex;
			justify-content: center;
			view{
				width: 150rpx;
				text-align: center;
			}
		}
		.xieyi{
			margin-top: 20rpx;
			font-size: 16rpx;
			color: #F1F1F1;
			text-align: center;
			checkbox{
				color: #000000;
				transform: scale(0.5);
				background-color: #EE0A24;
			}
			
		}
	}
	.login_pop{
		background-color: #FFFFFF;
		width: 500rpx;
		height: 400rpx;
		border-radius: 20rpx;
		padding: 10rpx 20rpx;
		label{
			font-size: 25rpx;
		}
		input{
			border: 1rpx solid #b1b1b1;
			border-radius: 10rpx;
			height: 60rpx;
			margin-top: 20rpx;
		}
		.btn_login_box{
			margin-top: 50rpx;
			display: flex;
			justify-content: space-around;
			button{
				width: 120rpx;
				font-size: 25rpx;
			}
		}
	}
</style>
